// 表单验证规则
export const rules = {
  // 必填验证
  required: (message = '此项为必填项') => ({
    required: true,
    message,
    trigger: 'blur'
  }),

  // 邮箱验证
  email: (message = '请输入正确的邮箱地址') => ({
    type: 'email',
    message,
    trigger: ['blur', 'change']
  }),

  // 手机号验证
  phone: (message = '请输入正确的手机号') => ({
    pattern: /^1[3-9]\d{9}$/,
    message,
    trigger: 'blur'
  }),

  // 身份证验证
  idCard: (message = '请输入正确的身份证号') => ({
    pattern: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
    message,
    trigger: 'blur'
  }),

  // 密码验证
  password: (message = '密码长度应为6-20位') => ({
    min: 6,
    max: 20,
    message,
    trigger: 'blur'
  }),

  // 强密码验证
  strongPassword: (message = '密码必须包含大小写字母、数字和特殊字符，长度8-20位') => ({
    pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,20}$/,
    message,
    trigger: 'blur'
  }),

  // 用户名验证
  username: (message = '用户名只能包含字母、数字和下划线，长度3-20位') => ({
    pattern: /^[a-zA-Z0-9_]{3,20}$/,
    message,
    trigger: 'blur'
  }),

  // 数字验证
  number: (message = '请输入数字') => ({
    type: 'number',
    message,
    trigger: 'blur'
  }),

  // 整数验证
  integer: (message = '请输入整数') => ({
    pattern: /^\d+$/,
    message,
    trigger: 'blur'
  }),

  // 正整数验证
  positiveInteger: (message = '请输入正整数') => ({
    pattern: /^[1-9]\d*$/,
    message,
    trigger: 'blur'
  }),

  // URL验证
  url: (message = '请输入正确的URL地址') => ({
    pattern: /^https?:\/\/.+/,
    message,
    trigger: 'blur'
  }),

  // 长度验证
  length: (min, max, message) => ({
    min,
    max,
    message: message || `长度应在${min}-${max}个字符之间`,
    trigger: 'blur'
  }),

  // 自定义验证
  custom: (validator, message = '验证失败') => ({
    validator: (rule, value, callback) => {
      if (validator(value)) {
        callback()
      } else {
        callback(new Error(message))
      }
    },
    trigger: 'blur'
  })
}

// 常用验证函数
export const validators = {
  // 验证邮箱
  isEmail: (email) => {
    const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
    return re.test(email)
  },

  // 验证手机号
  isPhone: (phone) => {
    const re = /^1[3-9]\d{9}$/
    return re.test(phone)
  },

  // 验证身份证
  isIdCard: (idCard) => {
    const re = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
    return re.test(idCard)
  },

  // 验证密码强度
  isStrongPassword: (password) => {
    const re = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,20}$/
    return re.test(password)
  },

  // 验证用户名
  isUsername: (username) => {
    const re = /^[a-zA-Z0-9_]{3,20}$/
    return re.test(username)
  },

  // 验证URL
  isUrl: (url) => {
    const re = /^https?:\/\/.+/
    return re.test(url)
  },

  // 验证IP地址
  isIP: (ip) => {
    const re = /^(\d{1,3}\.){3}\d{1,3}$/
    if (!re.test(ip)) return false
    
    return ip.split('.').every(segment => {
      const num = parseInt(segment, 10)
      return num >= 0 && num <= 255
    })
  },

  // 验证MAC地址
  isMAC: (mac) => {
    const re = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/
    return re.test(mac)
  },

  // 验证银行卡号
  isBankCard: (cardNumber) => {
    const re = /^\d{16,19}$/
    return re.test(cardNumber)
  },

  // 验证中文姓名
  isChineseName: (name) => {
    const re = /^[\u4e00-\u9fa5]{2,10}$/
    return re.test(name)
  },

  // 验证英文姓名
  isEnglishName: (name) => {
    const re = /^[a-zA-Z\s]{2,50}$/
    return re.test(name)
  },

  // 验证QQ号
  isQQ: (qq) => {
    const re = /^[1-9]\d{4,10}$/
    return re.test(qq)
  },

  // 验证微信号
  isWechat: (wechat) => {
    const re = /^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/
    return re.test(wechat)
  },

  // 验证车牌号
  isLicensePlate: (plate) => {
    const re = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/
    return re.test(plate)
  },

  // 验证统一社会信用代码
  isSocialCreditCode: (code) => {
    const re = /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/
    return re.test(code)
  }
}

// 表单验证辅助函数
export const formValidation = {
  // 创建验证规则
  createRules: (fields) => {
    const validationRules = {}
    
    for (const [field, ruleConfig] of Object.entries(fields)) {
      validationRules[field] = Array.isArray(ruleConfig) ? ruleConfig : [ruleConfig]
    }
    
    return validationRules
  },

  // 验证单个字段
  validateField: async (formRef, field) => {
    try {
      await formRef.validateField(field)
      return { valid: true }
    } catch (error) {
      return { valid: false, error }
    }
  },

  // 验证整个表单
  validateForm: async (formRef) => {
    try {
      await formRef.validate()
      return { valid: true }
    } catch (error) {
      return { valid: false, errors: error }
    }
  },

  // 清除验证
  clearValidation: (formRef, fields) => {
    if (fields) {
      formRef.clearValidate(fields)
    } else {
      formRef.clearValidate()
    }
  },

  // 重置表单
  resetForm: (formRef) => {
    formRef.resetFields()
  }
}

// 常用正则表达式
export const patterns = {
  email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
  phone: /^1[3-9]\d{9}$/,
  idCard: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
  password: /^.{6,20}$/,
  strongPassword: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,20}$/,
  username: /^[a-zA-Z0-9_]{3,20}$/,
  url: /^https?:\/\/.+/,
  ip: /^(\d{1,3}\.){3}\d{1,3}$/,
  mac: /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/,
  bankCard: /^\d{16,19}$/,
  chineseName: /^[\u4e00-\u9fa5]{2,10}$/,
  englishName: /^[a-zA-Z\s]{2,50}$/,
  qq: /^[1-9]\d{4,10}$/,
  wechat: /^[a-zA-Z][-_a-zA-Z0-9]{5,19}$/,
  licensePlate: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/,
  socialCreditCode: /^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$/
}
